package com.peng.edu.controller;



import com.peng.edu.entity.Chapter;
import com.peng.edu.entity.chapter.ChapterVo;
import com.peng.edu.service.ChapterService;
import com.peng.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author Dengp
 * @since 2022-06-16
 */
@RestController
@RequestMapping("/edu/chapter")
@CrossOrigin
public class ChapterController {
    @Autowired
    private ChapterService chapterService;

    //获取课程大纲列表，根据课程id进行查询
    @GetMapping("/getChapterVideo/{courseId}")
    public Result getChapterVideo(@PathVariable String courseId){
        List<ChapterVo> list= chapterService.getChapterVideo(courseId);
        HashMap<String, Object> map = new HashMap<String,Object>();
        map.put("list",list);
        return Result.ok().data(map);
    }

    //添加章节
    @PostMapping("addChapter")
    public Result addChapter(@RequestBody Chapter chapter){
        chapterService.save(chapter);
        return Result.ok();
    }

    //根据章节id查询
    @GetMapping("getChapter/{chapterId}")
    public Result getChapter(@PathVariable String chapterId){
        Chapter chapter = chapterService.getById(chapterId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("chapter",chapter);
        return Result.ok().data(map);
    }

    //修改章节
    @PostMapping("updateChapter")
    public Result updateChapter(@RequestBody Chapter chapter){
        chapterService.updateById(chapter);
        return Result.ok();
    }

    //删除章节
    @DeleteMapping("deleteById/{chapterId}")
    public Result deleteById(@PathVariable String chapterId){
        boolean flag = chapterService.deleteChapter(chapterId);
        return flag?Result.ok():Result.error();

    }

}

